package com.shaozi.nc.manage;

import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.SchedulerException;
import org.springframework.util.StringUtils;

import com.shaozi.common.schedule.DyncQuartzManage;
import com.shaozi.data.mq.business.base.SourceTypeConsts;
import com.shaozi.lib.manage.ManageBase;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.data.model.CompanyInfoModel;
import com.shaozi.nc.data.model.OrgPushMsgModel;
import com.shaozi.nc.data.mongodao.OrgPushMsgMDao;
import com.shaozi.nc.manage.rules.WorkReportCompRules;
import com.shaozi.nc.quartz.job.OrgPushJob;
import com.shaozi.utils.common.BeanUtils;

/**
 * BeanUtils.getBean("orgPushMsgMgr");
 * 
 * @author brenx
 * 
 */
public class OrgPushMsgManage extends ManageBase {

	private final Logger logger = Logger.getLogger(OrgPushMsgManage.class);

	public OrgPushMsgManage() {
	}

	public void updateCompany(CompanyInfoModel compModel) {
//		OrgPushMsgMDao dao = BeanUtils.getBean(OrgPushMsgMDao.class);
//		OrgPushMsgModel model = dao.getByCompId(compModel.getCompanyId());
//		if (model != null) {
//			scheduleOrgPush(compModel.getCompanyId(), model);
//			return;
//		}
//		model = new OrgPushMsgModel();
//		model.setCompId(compModel.getCompanyId());
//		model.setLevelType(SCConsts.LevelType.Comp);
//		buildCompDefaultRules(model);
//
//		addOrgPushMsg(compModel.getCompanyId(), model);
	}

	private String addOrgPushMsg(String compId, OrgPushMsgModel model) {
		OrgPushMsgMDao dao = BeanUtils.getBean(OrgPushMsgMDao.class);
		String _id = dao.addOrgPushMsg(compId, model);
		if (StringUtils.isEmpty(_id))
			return null;
		model.setId(_id);
		scheduleOrgPush(compId, model);
		return _id;
	}

	public void upsertOrgPushMsg(String compId, OrgPushMsgModel model) {
		OrgPushMsgMDao dao = BeanUtils.getBean(OrgPushMsgMDao.class);
		dao.upsertByUnique(compId, model);
		scheduleOrgPush(compId, model);
	}

	public List<OrgPushMsgModel> loadModels(String compId, int skip, int limit) {
		OrgPushMsgMDao dao = BeanUtils.getBean(OrgPushMsgMDao.class);
		return dao.loadAll(compId, skip, limit);
	}

	private void buildCompDefaultRules(OrgPushMsgModel model) {
		WorkReportCompRules reportRule = new WorkReportCompRules();
		model.setPushRegex(reportRule.buildDefaultWritePush(model.getCompId()));
		model.setTitle(reportRule.buildDefaultWriteTitle(model.getCompId()));
		model.setContent(reportRule.buildDefaultWriteContent(model.getCompId()));
		model.setSourceType(SourceTypeConsts.OA.Report.Alarm_New);
	}

	private void scheduleOrgPush(String compId, OrgPushMsgModel model) {
		DyncQuartzManage quartzMgr = (DyncQuartzManage) BeanUtils.getBean("dyncQuartzMgr");
		try {
			String groupId = getGroupId(compId, model.getSourceType());
			quartzMgr.upsertJobTime(groupId, model.getId(), OrgPushJob.class, groupId, model.getId(),
					model.getPushRegex());
		} catch (SchedulerException e) {
			logger.error(SzException.getStackMessage(e));
		}
	}

	private String getGroupId(String compId, int pushType) {
		return String.format("%s_%d", compId, pushType);
	}
}
